package com.ruoyi.record.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.record.common.RecordConstant;
import com.ruoyi.record.domain.StepStudentVO;
import com.ruoyi.record.service.IWordStudyStepStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.ruoyi.record.mapper.WordStudyStepMapper;
import com.ruoyi.record.domain.WordStudyStep;
import com.ruoyi.record.service.IWordStudyStepService;

/**
 * 单词学习任务Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-03
 */
@Service
public class WordStudyStepServiceImpl implements IWordStudyStepService 
{
    @Autowired
    private WordStudyStepMapper wordStudyStepMapper;

    private IWordStudyStepStudentService studentService;

    public WordStudyStepServiceImpl(@Lazy IWordStudyStepStudentService studentService) {
        this.studentService = studentService;
    }

    /**
     * 查询单词学习任务
     * 
     * @param id 单词学习任务主键
     * @return 单词学习任务
     */
    @Override
    public WordStudyStep selectWordStudyStepById(Long id)
    {
        return wordStudyStepMapper.selectWordStudyStepById(id);
    }


    /**
     * 查询单词学习任务列表
     * 
     * @param wordStudyStep 单词学习任务
     * @return 单词学习任务
     */
    @Override
    public List<WordStudyStep> selectWordStudyStepList(WordStudyStep wordStudyStep)
    {
        return wordStudyStepMapper.selectWordStudyStepList(wordStudyStep);
    }

    /**
     * 筛选过期的任务数据
     * */
    @Override
    public List<WordStudyStep> selectStepList(WordStudyStep wordStudyStep)
    {
        return wordStudyStepMapper.selectStepList(wordStudyStep);
    }

    /**
     * 新增单词学习任务
     * 
     * @param wordStudyStep 单词学习任务
     * @return 结果
     */
    @Override
    public int insertWordStudyStep(WordStudyStep wordStudyStep)
    {
        wordStudyStep.setTime(DateUtils.getNowDate());
        wordStudyStep.setStatus(RecordConstant.STATUS_NORMAL);
        wordStudyStep.setActive(RecordConstant.ACTIVE_READY);
        wordStudyStepMapper.insertWordStudyStep(wordStudyStep);
        return Math.toIntExact(wordStudyStep.getId());
    }

    @Override
    public String updateStepStudentData(StepStudentVO stepStudentVO){
        WordStudyStep studyStep = new WordStudyStep();
        Integer main = stepStudentVO.getId();
        studyStep.setId(Long.valueOf(main));
        ArrayList<String> studentNameList = new ArrayList<>();
        ArrayList<String> studentIdList = new ArrayList<>();
        studentService.selectStudentListByStep(Long.valueOf(main)).forEach(student -> {
            Integer studentId = student.getStudentId();
            String studentName = student.getStudentName();
            studentNameList.add(studentName);
            studentIdList.add(String.valueOf(studentId));
        });
        studyStep.setStudentName(String.join(",", studentNameList));
        studyStep.setStudentId(String.join(",",studentIdList));
        studyStep.setComplete(studentNameList.size());
        wordStudyStepMapper.updateWordStudyStep(studyStep);
        return studyStep.getStudentId() + "-" + studyStep.getStudentName();
    }

    /**
     * 修改单词学习任务
     * 
     * @param wordStudyStep 单词学习任务
     * @return 结果
     */
    @Override
    public int updateWordStudyStep(WordStudyStep wordStudyStep)
    {
        return wordStudyStepMapper.updateWordStudyStep(wordStudyStep);
    }

    /**
     * 批量删除单词学习任务
     * 
     * @param ids 需要删除的单词学习任务主键
     * @return 结果
     */
    @Override
    public int deleteWordStudyStepByIds(Long[] ids)
    {
        return wordStudyStepMapper.deleteWordStudyStepByIds(ids);
    }

    /**
     * 删除单词学习任务信息
     * 
     * @param id 单词学习任务主键
     * @return 结果
     */
    @Override
    public int deleteWordStudyStepById(Long id)
    {
        return wordStudyStepMapper.deleteWordStudyStepById(id);
    }
}
